﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTServices.Entities;
using GTServices.WPF.Client.ViewModels;
using Caliburn.Micro;
using GTServices.Model;
using System.Windows.Data;
using GTServices.WPF.Client.Interfaces;
namespace GTServices.WPF.Client.ViewModels.Entities
{
    public class PatternViewModel : PropertyChangedBase, ISelectable, IEntityViewModel
    {
      
        public Pattern _pattern;
        public Pattern Pattern
        { 
            get { return _pattern; } 
            set 
            { 
                _pattern = value;
                NotifyOfPropertyChange(() => Pattern);
            }
        }
        public String PatternStr
        {
            get { return Pattern.PatternStr; }
            set
            {
                Pattern.PatternStr = value;
                NotifyOfPropertyChange(() => this.PatternStr);
            }
        }
        public Language Language { 
            get { return Pattern.Language; }
            set { Pattern.Language = value; 
                NotifyOfPropertyChange(() => this.Language); 
            }
        }
        public String ReferenceUrl
        {
            get { return Pattern.ReferenceUrl; }
            set
            {
                Pattern.ReferenceUrl = value;
                NotifyOfPropertyChange(() => this.ReferenceUrl);
            }
        }
        public String Recommendation
        {
            get { return Pattern.Recommendation; }
            set
            {
                Pattern.Recommendation = value;
                NotifyOfPropertyChange(() => this.Recommendation);
            }
        }
        public String PatternInfo
        {
            get { return Pattern.PatternInfo; }
            set
            {
                Pattern.PatternInfo = value;
                NotifyOfPropertyChange(() => this.PatternInfo);
            }
        }

        private bool _isSelected;
        public bool IsSelected
        {

            get { return _isSelected; }
            set
            {
                _isSelected = value;
                NotifyOfPropertyChange(() => IsSelected);
            }
        }
        public object Model
        {
            get { return Pattern; }
            set
            {
                if (value is Pattern)
                    Pattern = value as Pattern;
            }
        }

        public String CategoriesAsString
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (Category c in this.Pattern.Categories)
                {
                    sb.AppendLine(c.Name); 
                }
                return sb.ToString(); 
            }
        }

        public String FirstCategoryIfExists
        {
            get
            {
                Category i = Pattern.Categories.FirstOrDefault();
                if (i != null)
                {
                    return i.Name;
                }
                else
                {
                    return "(None)";
                }
            }
        }


        public PatternViewModel()
        {
          
        }

        public void SelectLanguage()
        {
            var languages = ModelViewModels.Instance.LanguageViewModels.ToList(); 

            ListPickerViewModel lpvm = new ListPickerViewModel(languages.ToList<Object>(), "Language.Name", "Language.Name"); 

            if (GTWindowManager.Instance.ShowDialog(lpvm, 300, 300) == true && lpvm.SelectedItem != null)
            {
                this.Language = (lpvm.SelectedItem as LanguageViewModel).Language;
            }
        }
    }
}
